import { defineStore } from 'pinia';
import { getUser } from '@/api/user';
import { removeToken } from '@/utils/token';

export const useUserStore = defineStore('user', {
  state() {
    return {
      userInfo: {},
      permissions: [],
    };
  },
  getters: {
    userId() {
      return this.userInfo?.id;
    },
    name() {
      return this.userInfo?.name;
    },
    avatar() {
      return this.userInfo?.avatar;
    },
    role() {
      return this.userInfo?.role || [];
    },
    getPermissions() {
      return this.userInfo?.role || [];
    },
  },
  actions: {
    async getUserInfo() {
      try {
        const res = await getUser();
        if (res.code === 0) {
          const { id, name, avatar, role } = res.data;
          this.userInfo = { id, name, avatar, role };
          console.log(this.userInfo);
          return Promise.resolve(res.data);
        } else {
          return Promise.reject(res.message);
        }
      } catch (error) {
        console.error(error);
        return Promise.reject(error.message);
      }
    },
    logout() {
      removeToken();
      this.userInfo = {};
    },
    setUserInfo(userInfo = {}) {
      this.userInfo = { ...this.userInfo, ...userInfo };
    },
  },
});
